//
//  PaperQuestion.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/7/25.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperQuestion.h"
#import "PaperDataManager.h"
#import "PaperModel.h"
#import "PaperItem.h"

@implementation PaperQuestion

-(instancetype)init{
    if (self = [super init]) {
        _scores              = @[@"0%",@"20%",@"40%",@"60%",@"80%",@"100%"];
        _scoreMarkedIndex    = -1;
        _options             = [NSMutableArray new];
        _userOptions         = [NSMutableArray new];
        _subQuestions = [NSMutableArray new];
    }
    return self;
}

-(instancetype)initWithDict: (NSDictionary*)dict{
    if (self = [self init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

-(void)setValue:(id)value forKey:(NSString *)key{
    if ([key isEqualToString:@"options"]) {
        for (NSDictionary *dict in value[@"option"]) {
            if (dict.allKeys.count > 0) {
                PaperQuestionOption *op = [[PaperQuestionOption alloc]initWithDict:dict];
                [_options addObject: op];
            }
        }
        return; // WARN
    }
    // 试题内容
    else if ([key isEqualToString:@"questionContent"]){
        _questionContent = [self replaceHtmlTag:@"width=\"100%\"" withString:@"" inString:value[@"text"]];
        return;
    }
    // 试题分析
    else if ([key isEqualToString:@"analysis"]){
        _analysis = [self replaceHtmlTag:@"width=\"100%\"" withString:@"" inString:value[@"text"]];
        return;
    }
    // 标准答案
    
    [super setValue:value[@"text"] forKey:key];
}

#pragma mark - 替换题目/选项中 'width=100%'
-(NSString*)replaceHtmlTag: (NSString*)tag
                withString: (NSString*)replace
                  inString: (NSString*)content{

    if ([content containsString:tag]) {
        return [content stringByReplacingOccurrencesOfString:tag withString:replace options:NSCaseInsensitiveSearch range:NSMakeRange(0, content.length-1)];
//        NSLog(@"question content after replacing is: %@",qstContent);
    }
    return content;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}

-(NSString *)questionContent{
    if (![_parentid isEqualToString:@"0"]) { //子题
        return [NSString stringWithFormat:@"%@（%d）%@%@",@"<html><body style=\"margin:0;\"><div id=\"content\" style=\"word-break:break-word;font-size:18px;\">",_questionNum,_questionContent,@"</div></body></html>"];
    }
    NSString *html = [NSString stringWithFormat:@"%@【%@】%d、 %@%@",@"<html><body style=\"margin:0;\"><div id=\"content\" style=\"word-break:break-word;font-size:18px;\">",_typeName, _questionNum, _questionContent,@"</div></body></html>"];
    return html;
}


#pragma mark - getters
-(NSString *)userAnswer{
    if (!_userAnswer && _userOptions.count > 0) { //选择题
        NSMutableString *a = [NSMutableString string];
        for (NSNumber *i in _userOptions) {
            PaperQuestionOption *op = _options[i.integerValue];
            NSString *answer = op.optionIndex ? op.optionIndex : op.text;
            [a appendString: answer];
        }
        _userAnswer = a;
    }
//    NSLog(@"%@",_userAnswer);
    return _userAnswer; //输入题
}

-(BOOL)right{
    return _right = [_userAnswer isEqualToString:_answer];
}

-(void)setMarkedScore:(NSString *)score{
    if (score && ![score isEqualToString:@""]) {
        for (int i=0; i<_scores.count; i++) {
            if ([_scores[i] isEqualToString:score]) {
                _scoreMarkedIndex = i;
                break;
            }
        }
    }
}

-(NSString *)qstId{
    if (_docID && !_qstId) {
        return _docID;
    }
    return _qstId;
}

-(NSDictionary *)dictionary{
    NSMutableArray *ops = [NSMutableArray array];
    for (PaperQuestionOption *op in _options) {
    }
    NSDictionary *dic = @{
          @"orderid": _orderid ? _score : @"",
          @"qstId": _qstId ? _qstId : @"",
          @"tqId": _qstId ? _qstId : @"",
          @"docID": _docID ? _docID : @"",
          @"qtype": _qtype ? _qtype : @"",
          @"parentid": _parentid ? _parentid : @"",
          @"time": _time ? _time : @"",
          @"score": _score ? _score : @"",
          @"difficultId": _difficultId ? _difficultId : @"",
          @"difficultName": _difficultName ? _difficultName : @"",
          @"categoryId": _categoryId ? _categoryId : @"",
          @"categoryName": _categoryName ? _categoryName : @"",
          @"has_sub": _has_sub ? _has_sub : @"",
          @"answer": _answer ? _answer : @"",
          @"analysis": _analysis ? _analysis : @"",
          @"questionContent": _questionContent ? _questionContent : @"",
          @"options": ops,
          // 另加用户的答案
          @"userAnswer": _userAnswer ? _userAnswer : @""
    };
    return dic;
}

@end
